﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2012/2/16 19:59:56
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Restaurant.EntityType;
using Sector7.Framework.Core;
using System.Data.SqlClient;
using Sector7.Framework.Utility;
using Sector7.Restaurant.DAO.Utility;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    /// <summary>
    /// 菜单数据访问对象
    /// </summary>
    public class MenuItemDAO : IMenuItemDAO
    {
        #region Public functions
        public int Create(RS_MenuItem menuItem)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql = @"
                    INSERT INTO RS_MenuItem
                            (MenuItemId
                            ,MetaDataId
                            ,UnitId
                            ,UnitPrice
                            ,Description
                            ,LabelIds)
                        VALUES
                            (@MenuItemId
                            ,@MetaDataId
                            ,@UnitId
                            ,@UnitPrice
                            ,@Description
                            ,@LabelIds)";

                return opt.ExecuteNonQuery(
                    sql,
                    DBUtil.CreateSqlParameter(menuItem.MenuItemId, "MenuItemId"),
                    DBUtil.CreateSqlParameter(menuItem.MetadataId, "MetaDataId"),
                    DBUtil.CreateSqlParameter(menuItem.UnitId, "UnitId"),
                    DBUtil.CreateSqlParameter(menuItem.UnitPrice, "UnitPrice"),
                    DBUtil.CreateSqlParameter(menuItem.Description, "Description"),
                    DBUtil.CreateSqlParameter(menuItem.LabelIds, "LabelIds"));
            }
        }

        public List<RS_MenuItem> ReadAll(int typeId)
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_MenuItem> menuItems = new List<RS_MenuItem>();
                List<SqlParameter> sqlParameters = new List<SqlParameter>();

                string sql =
                    "SELECT *                                                               " +
                    "   FROM RS_MenuItem AS menu                                            " +
                    "       LEFT JOIN RS_MetaData AS metaData                               " +
                    "           ON menu.MetaDataId = metaData.Id                            " +
                    "       LEFT JOIN RS_Unit AS unit                                       " +
                    "           ON menu.UnitId = unit.UnitId                                " +
                    "   WHERE metaData.TypeId = @TypeId                                     ";

                SqlDataReader reader = opt.ExecuteQuery(
                    sql,
                    DBUtil.CreateSqlParameter(typeId, "TypeId"));

                while (reader.Read())
                {
                    object menuItem = new RS_MenuItem();
                    object metaData = new RS_Metadata();
                    object unit = new RS_Unit();

                    DBUtil.ReadData(reader, ref menuItem);
                    DBUtil.ReadData(reader, ref metaData);
                    DBUtil.ReadData(reader, ref unit);

                    (menuItem as RS_MenuItem).Metadata = metaData as RS_Metadata;
                    (menuItem as RS_MenuItem).Unit = unit as RS_Unit;

                    IMenuItemLabelDAO menuItemLabelDAO = new MenuItemLabelDAO();
                    (menuItem as RS_MenuItem).Labels = menuItemLabelDAO.ReadAll((menuItem as RS_MenuItem).LabelIds);

                    menuItems.Add(menuItem as RS_MenuItem);
                }

                return menuItems;
            }
        }

        public int Update(EntityDelta delta)
        {
            using (DBHelper opt = new DBHelper())
            {
                return opt.ExecuteNonQuery(
                    DBUtil.CreateUpdateSql(delta),
                    DBUtil.CreateUpdateParameters(delta));
            }
        }

        /*******************************************************************************/

        public RS_MenuItem Read(string menuItemId)
        {
            using (DBHelper opt = new DBHelper())
            {
                object menuItem = null;
                List<SqlParameter> sqlParameters = new List<SqlParameter>();
                string sql =
                    "SELECT *                                                               " +
                    "   FROM RS_MenuItem AS menu                                            " +
                    "       LEFT JOIN RS_MetaData AS metaData                               " +
                    "           ON menu.MetaDataId = metaData.Id                            " +
                    "       LEFT JOIN RS_Unit AS unit                                       " +
                    "           ON menu.UnitId = unit.UnitId                                " +
                    "   WHERE menu.MenuItemId = @MenuItemId                             ";

                SqlDataReader reader = opt.ExecuteQuery(
                    sql,
                    DBUtil.CreateSqlParameter(menuItemId, "MenuItemId"));

                if (reader.Read())
                {
                    menuItem = new RS_MenuItem();
                    object metaData = new RS_Metadata();
                    object unit = new RS_Unit();

                    DBUtil.ReadData(reader, ref menuItem);
                    DBUtil.ReadData(reader, ref metaData);
                    DBUtil.ReadData(reader, ref unit);
                    (menuItem as RS_MenuItem).Metadata = metaData as RS_Metadata;
                    (menuItem as RS_MenuItem).Unit = unit as RS_Unit;
                }

                return menuItem as RS_MenuItem;
            }
        }

        public int Delete(string menuItemId)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    "DELETE FROM RS_MenuItem            " +
                    "   WHERE MenuItemId = @MenuItemId  ";

                return opt.ExecuteNonQuery(
                    sql,
                    DBUtil.CreateSqlParameter(menuItemId, "MenuItemId"));
            }
        }
        #endregion
    }
}
